package de.tabuma.pm.security.controller;
import de.tabuma.pm.projectnavigation.entity.*;
import de.tabuma.ssoclient.entity.Privileg;
import de.tabuma.pm.security.*;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import de.tabuma.lib.*;
import javax.servlet.*;
import javax.servlet.http.*;
import de.tabuma.ssoclient.entity.*;
import de.tabuma.ssoclient.logik.*;
import com.google.appengine.api.users.*;
import de.tabuma.pm.*;
/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1.4.1 $
 * @since $Date: 2010/01/13 10:19:31 $
 */
public class GroupPrivilegController extends tabumacontroller {

    private static Logger log = Logger.getLogger(GroupPrivilegController.class.getName());

    public GroupPrivilegController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        try {
            String projectId = request.getParameter(de.tabuma.pm.constants.projectId);
            int projectIdInt = Integer.valueOf(projectId).intValue();

            String groupId = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_GROUP_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int groupIdInt = Integer.valueOf(groupId).intValue();

            List<Privileg> pList = new ArrayList<Privileg>();

            String page = "Page";
            List<ProjectNavigation> pnList = de.tabuma.pm.projectnavigation.logik.ProjectNavigationFacade.getList(projectIdInt);

            String id = request.getParameter(de.tabuma.pm.constants.SECURITY_PROVIDER_PARAM+de.tabuma.lib.contentmanagement.constants.id);
            int intId = Integer.valueOf(id).intValue();


            String ssoId = SecurityProviderInstanceFacade.getSingleSignOnId(intId);
            SecurityProviderInstance spi = SecurityProviderInstanceFacade.get(intId);

            List<Privileg> spList = PrivilegFacade.getList(spi.getSsoProjectId(), ssoId);
            if(spList==null) {
                spList = new ArrayList<Privileg>();
            }

            for(int i=0;i<pnList.size();i++) {
                ProjectNavigation pn = pnList.get(i);
                String pnName = page + " " + pn.getName()+" ("+pn.getId()+")";
                boolean add = true;
                for(int j=0;j<spList.size();j++) {
                    Privileg privileg = spList.get(j);
                    if(privileg.getName().indexOf("("+pn.getId()+")")!=-1) {
                        add = false;
                        j = spList.size();
                        if(!privileg.getName().equals(pnName)) {
                            privileg.setName(pnName);
                            privileg.setDescription(pnName);
                            PrivilegFacade.update(privileg, ssoId);
                        }
                        pList.add(privileg);
                    }
                }
                if(add) {
                    Privileg privileg = new Privileg();
                    privileg.setAdmin(false);
                    privileg.setName(pnName);
                    privileg.setDescription(pnName);
                    privileg.setProjectId(spi.getSsoProjectId());
                    privileg.setArt(0);
                    int privilegId = PrivilegFacade.create(privileg, ssoId);
                    Long long1 = new Long(privilegId);
                    privileg.setId(long1.longValue());
                    pList.add(privileg);
                }
            }
            request.setAttribute(constants.privilegList, pList);

            List<GroupPrivileg> gpList = GroupPrivilegFacadeWrapper.getList(groupIdInt, ssoId);
            if(request.getParameter(constants.privilegSave)!=null) {

                UserService userService = UserServiceFactory.getUserService();
                com.google.appengine.api.users.User user = userService.getCurrentUser();

                request.setAttribute(constants.privilegSaved, "true");
                for(int i=0;i<pList.size();i++) {
                    Privileg p = pList.get(i);
                    if(request.getParameter(constants.privilegId+p.getId().intValue())!=null) {
                        boolean isIncludedInList = false;
                        for(int j=0;j<gpList.size();j++) {
                            if(gpList.get(j).getPrivilegId()==p.getId()) {
                                isIncludedInList = true;
                            }
                        }
                        if(!isIncludedInList) {
                            GroupPrivileg gp = new GroupPrivileg();
                            gp.setGroupId(groupIdInt);
                            gp.setPrivilegId(p.getId().intValue());
                            GroupPrivilegFacadeWrapper.create(gp, user.getEmail(), ssoId);
                        }
                    }
                    else {
                        for(int j=0;j<gpList.size();j++) {
                            if(gpList.get(j).getPrivilegId()==p.getId()) {
                                GroupPrivileg gp = gpList.get(j);
                                gpList.remove(j);
                                GroupPrivilegFacadeWrapper.remove(gp.getId().intValue(), user.getEmail(), ssoId);
                            }
                        }
                    }
                }
            }

            request.setAttribute(constants.groupPrivilegList, gpList);

            GroupPrivilegView gpv = new GroupPrivilegView(this.getServlet(), this.getParameter());
            gpv.processRequest(context, request, response, out);
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.severe(warning);
        }
    }
}